/**
 *
 * @author carolina
 */
class DevicesMain {

    private boolean analyzed = false
    def idSession = null;
    def alldevices = null;

    def getContents = { login, pass  ->
         try {
            if (analyzed) {
            } else {
                alldevices = conectaEmbeddedDevices("http://tomcat.embedded.ufcg.edu.br/library/wSRestLibrary/authentication?login=" + login + "&password=" + pass)
                analyzed = true
             }
             if (alldevices.size() == 0){
                 alldevices = "Você não tem Devices!"
             }
              return alldevices
         } catch (Exception e) {
             println "Message: " + e.getMessage()
             return  "Você não tem Devices!"
         }
    }

    private def conectaEmbeddedDevices = { connect ->
        //def text = getContent(content)
        //idSession = getIdSession(text)
        def result = this.getDevices("src/embeddedDevices/getPendingLoaningsDevicesByUserOk.xml")
        //newsLogout()
        return result
    }

    def getContent = { connect ->
        println "connect: " + connect
        def url = new URL(connect)
        def connection = url.openConnection()
        connection.setRequestMethod("POST")
        connection.connect()

        def result = ""
        if (connection.responseCode != 200) {
            result = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><WSResultsRestLibrary><class>WSResultsRestLibrary</class><error>Bad Reponse</error><sessionID/><sucessMessage/></WSResultsRestLibrary>"
        } else {
            result = connection.content.text
       }       
       return result
    }

    private def getIdSession = { content ->
        def xml = new XmlParser().parseText(content)
        def result = "-1"
        xml.each {
            if (it.name() == "sessionID" ) {
                result = it.text()
            }
        }
        return result
    }

     private def getDevices = { content ->         
        //def text = getContent(content)
        //def xml = new XmlParser().parseText(text)
        def xml = new XmlParser().parse(content)

        def result = "";
         if (xml.class.text() == "WSResultsRestLibrary" ) {
            println "xml.error.text()=" + xml.error.text()
            result = "Você não tem devices!"
        } else {
            xml.loaning.each {
                //def result1 = getBookById("http://tomcat.embedded.ufcg.edu.br/devices/wSRestLibrary/getBookById?sessionId="
                //                            + idSession + "&bookId="+ it.book.'@id'[0])
                def result1 = getDevicesById("src/embeddedDevices/getDeviceByIdOk.xml")
                def result2 = it.loaningDate.text()
                result = result + "<p></p> Você esta com o device  <strong>" + result1 + "</strong> desde " + result2
            }
        }
        return result
    }

    def private newsLogout = {
       def text = getContent("http://tomcat.embedded.ufcg.edu.br/library/wSRestLibrary/logout?sessionId="
                                + idSession)
       def xml = new XmlParser().parseText(text)
       if (xml.class.text() == "WSResultsRestLibrary" ) {
       }
    }

   private def getDevicesById = { content ->
       //def text = getContent(content)
       //def device = new XmlParser().parseText(text)

       def device = new XmlParser().parse(content)
       if (device.class.text() == "WSResultsRestLibrary" ) {
           // throw new Exception(device.error.text())
        } else {            
            def result = ""
            device.each {                
                if (it.name() == "publicationYear") {
                   result = it.text()
                }
                if (it.name() == "title") {
                   result = it.text() + " - " + result
                }
            }
                return result
        }
   }

}

